libraries
library(dplyr)
library(ggplot2)
library(nnet)
library(psych)
library(corrplot)
library(tidyverse)
package 㤼㸱tidyverse㤼㸲 was built under R version 3.5.3[37m-- [1mAttaching packages[22m --------------------------------------- tidyverse 1.2.1 --[39m
[37m[32mv[37m [34mtibble [37m 2.1.3 [32mv[37m [34mpurrr [37m 0.3.2
[32mv[37m [34mtidyr [37m 0.8.3 [32mv[37m [34mstringr[37m 1.4.0
[32mv[37m [34mreadr [37m 1.3.1 [32mv[37m [34mforcats[37m 0.4.0[39m
package 㤼㸱tibble㤼㸲 was built under R version 3.5.3package 㤼㸱tidyr㤼㸲 was built under R version 3.5.3package 㤼㸱readr㤼㸲 was built under R version 3.5.3package 㤼㸱purrr㤼㸲 was built under R version 3.5.3package 㤼㸱stringr㤼㸲 was built under R version 3.5.3package 㤼㸱forcats㤼㸲 was built under R version 3.5.3[37m-- [1mConflicts[22m ------------------------------------------ tidyverse_conflicts() --
[31mx[37m [34mpsych[37m::[32m%+%()[37m masks [34mggplot2[37m::%+%()
[31mx[37m [34mpurrr[37m::[32maccumulate()[37m masks [34mforeach[37m::accumulate()
[31mx[37m [34mpsych[37m::[32malpha()[37m masks [34mggplot2[37m::alpha()
[31mx[37m [34mdplyr[37m::[32mbetween()[37m masks [34mdata.table[37m::between()
[31mx[37m [34mdplyr[37m::[32mcombine()[37m masks [34mrandomForest[37m::combine()
[31mx[37m [34mtidyr[37m::[32mexpand()[37m masks [34mMatrix[37m::expand()
[31mx[37m [34mdplyr[37m::[32mfilter()[37m masks [34mstats[37m::filter()
[31mx[37m [34mdplyr[37m::[32mfirst()[37m masks [34mdata.table[37m::first()
[31mx[37m [34mdplyr[37m::[32mlag()[37m masks [34mstats[37m::lag()
[31mx[37m [34mdplyr[37m::[32mlast()[37m masks [34mdata.table[37m::last()
[31mx[37m [34mpurrr[37m::[32mlift()[37m masks [34mcaret[37m::lift()
[31mx[37m [34mggplot2[37m::[32mmargin()[37m masks [34mrandomForest[37m::margin()
[31mx[37m [34mdplyr[37m::[32mslice()[37m masks [34mxgboost[37m::slice()
[31mx[37m [34mpurrr[37m::[32mtranspose()[37m masks [34mdata.table[37m::transpose()
[31mx[37m [34mpurrr[37m::[32mwhen()[37m masks [34mforeach[37m::when()[39m
Load data
data<- read.csv("C://Users/s137r4//Desktop//Analysisfile//Accident14_17.csv")
Warning: closing unused connection 16 (<-CHR5C99E.corp.gwpnet.com:11346)
Warning: closing unused connection 15 (<-CHR5C99E.corp.gwpnet.com:11346)
Warning: closing unused connection 14 (<-CHR5C99E.corp.gwpnet.com:11346)
Warning: closing unused connection 9 (<-CHR5C99E.corp.gwpnet.com:11346)
Warning: closing unused connection 8 (<-CHR5C99E.corp.gwpnet.com:11346)
Warning: closing unused connection 7 (<-CHR5C99E.corp.gwpnet.com:11346)
Warning: closing unused connection 6 (<-CHR5C99E.corp.gwpnet.com:11346)
data <- data[, -c(1)]
missing values
p<- function(x){sum(is.na(x))/length(x)*100}
apply(data, 2, p)
X Accident_Index Location_Easting_OSGR
0.000000000 0.000000000 0.010534305
Location_Northing_OSGR Longitude Latitude
0.010534305 0.006224816 0.006224816
Police_Force Accident_Severity Number_of_Vehicles
0.000000000 0.000000000 0.000000000
Number_of_Casualties Date Day_of_Week
0.000000000 0.000000000 0.000000000
Time Local_Authority_.District. Local_Authority_.Highway.
0.000000000 0.000000000 0.000000000
X1st_Road_Class X1st_Road_Number Road_Type
0.000000000 0.000000000 0.000000000
Speed_limit Junction_Detail Junction_Control
0.000000000 0.000000000 0.000000000
X2nd_Road_Class X2nd_Road_Number Pedestrian_Crossing.Human_Control
0.000000000 0.000000000 0.000000000
Pedestrian_Crossing.Physical_Facilities Light_Conditions Weather_Conditions
0.000000000 0.000000000 0.000000000
Road_Surface_Conditions Special_Conditions_at_Site Carriageway_Hazards
0.000000000 0.000000000 0.000000000
Urban_or_Rural_Area Did_Police_Officer_Attend_Scene_of_Accident LSOA_of_Accident_Location
0.000000000 0.000000000 0.000000000
Vehicle_Reference Vehicle_Type Towing_and_Articulation
0.000000000 0.000000000 0.000000000
Vehicle_Manoeuvre Vehicle_Location.Restricted_Lane Junction_Location
0.000000000 0.000000000 0.000000000
Skidding_and_Overturning Hit_Object_in_Carriageway Vehicle_Leaving_Carriageway
0.000000000 0.000000000 0.000000000
Hit_Object_off_Carriageway X1st_Point_of_Impact Was_Vehicle_Left_Hand_Drive.
0.000000000 0.000000000 0.000000000
Journey_Purpose_of_Driver Sex_of_Driver Age_of_Driver
0.000000000 0.000000000 0.000000000
Age_Band_of_Driver Engine_Capacity_.CC. Propulsion_Code
0.000000000 0.000000000 0.000000000
Age_of_Vehicle Driver_IMD_Decile Driver_Home_Area_Type
0.000000000 0.000000000 0.000000000
Casualty_Reference Casualty_Class Sex_of_Casualty
0.000000000 0.000000000 0.000000000
Age_of_Casualty Age_Band_of_Casualty Casualty_Severity
0.000000000 0.000000000 0.000000000
Pedestrian_Location Pedestrian_Movement Car_Passenger
0.000000000 0.000000000 0.000000000
Bus_or_Coach_Passenger Pedestrian_Road_Maintenance_Worker Casualty_Type
0.000000000 0.000000000 0.000000000
Casualty_Home_Area_Type Year
0.000000000 0.000000000
colz <- c(7,11,15:31,34:48, 50:67)
data[, colz]<-lapply(data[, colz], factor)
by_year_count <- data %>% select(Accident_Index, Year) %>% group_by(Year) %>% summarise(total.count=n())%>% mutate(total.count = total.count/sum(total.count)*100)
by_year_count
chart_year <- ggplot(data=by_year_count, aes(x=Year, y=total.count, fill=Year)) + geom_bar(stat="identity")+geom_text(aes(label=paste0(round(total.count, 2),"%")),position=position_dodge(width=0.9), vjust=-0.25)
chart_year
by_yearsev <- data %>% select(Accident_Index, Year, Accident_Severity) %>% group_by(Year, Accident_Severity) %>% summarise(total.count=n())%>% mutate(total.count = total.count/sum(total.count)*100)
by_yearsev
chart_yearsev <- ggplot(data=by_yearsev, aes(x=Year, y=total.count, fill=Accident_Severity)) + geom_bar(stat="identity")+geom_text(aes(label=paste0(round(total.count, 2),"%")),position=position_stack(vjust=0.25))
chart_yearsev
recoding
data <- data %>%
mutate(Vehicle_Manoeuvre_category=case_when(Vehicle_Manoeuvre %in% 1:5~"Low_speed_manoeuvre",
Vehicle_Manoeuvre %in% 6:10 ~ "Turning_Manoeuvre",
Vehicle_Manoeuvre %in% 11:15 ~ "Lane_change_Manoeuvre",
Vehicle_Manoeuvre %in% 16:18 ~ "Going_ahead_Manoeuvre"
))
manaouvre<- data %>% select(Accident_Index, Vehicle_Manoeuvre_category)%>% group_by(Vehicle_Manoeuvre_category) %>% summarise(t_count = n())%>% drop_na(Vehicle_Manoeuvre_category)%>% mutate(t_count = t_count/sum(t_count)*100)
manaouvre
manuevsev<- data %>% select(Accident_Index,Accident_Severity, Vehicle_Manoeuvre_category)%>% group_by(Vehicle_Manoeuvre_category, Accident_Severity) %>% summarise(t_count = n())%>% drop_na(Vehicle_Manoeuvre_category)%>% mutate(t_count = t_count/sum(t_count)*100)
manuevsev
data<-data%>%
filter(Urban_or_Rural_Area %in% 1:2)
data<-data%>%
filter(Light_Conditions %in% 1:7)
#1-highway 2- other roads
data <- data %>%
mutate(Highway=case_when( X1st_Road_Class %in% 1:2~ 1,
X1st_Road_Class %in% 3:6 ~ 0))
# 1 - daylight and 2- darkness
data <- data %>%
mutate(Light_Cond=case_when(Light_Conditions ==1 ~ 1,
Light_Conditions %in% 2:7 ~ 0))
light<- data %>% select(Accident_Index, Light_Cond)%>% group_by(Light_Cond) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
light
light1<- data %>% select(Accident_Index, Light_Cond,Accident_Severity)%>% group_by(Light_Cond,Accident_Severity) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
light1
table(data$Light_Cond)
0 1
121617 296039
table(data$Highway)
0 1
394659 22997
data <- data %>%
mutate(Weather_Cond=case_when(Weather_Conditions==1 ~1,
Weather_Conditions %in% 2:8 ~0
))
weather<- data %>% select(Accident_Index, Weather_Cond)%>% group_by(Weather_Cond) %>% summarise(t_count = n())%>% drop_na(Weather_Cond)%>% mutate(t_count = t_count/sum(t_count)*100)
weather
weather2<- data %>% select(Accident_Index, Weather_Cond,Accident_Severity)%>% group_by(Weather_Cond,Accident_Severity) %>% summarise(t_count = n())%>% drop_na(Weather_Cond) %>% mutate(t_count = t_count/sum(t_count)*100)
weather2
table(data$Highway)
0 1
394659 22997
data <- data %>%
mutate(Road_Surface_Cond=case_when(Road_Surface_Conditions==1 ~1,
Road_Surface_Conditions %in% 2:7 ~0
))
#1- dry and 2- wet
table(data$Road_Surface_Cond)
0 1
129537 286113
Road_Sc<- data %>% select(Accident_Index,Road_Surface_Cond)%>% group_by(Road_Surface_Cond)%>% drop_na(Road_Surface_Cond) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
Road_Sc
Road_Sc1<- data %>% select(Accident_Index,Road_Surface_Cond,Accident_Severity)%>% group_by(Road_Surface_Cond,Accident_Severity)%>% drop_na(Road_Surface_Cond) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
Road_Sc1
data <- data %>%
mutate(Area=case_when(Urban_or_Rural_Area==1& Highway==1 ~ "Highway",
Urban_or_Rural_Area==2& Highway==1 ~"Highway",
Urban_or_Rural_Area==1& Highway==0 ~ "Urban",
Urban_or_Rural_Area==2& Highway==0 ~ "Rural"))
table(data$Area)
Highway Rural Urban
22997 149779 244880
areads<- data %>% select(Accident_Index,Area)%>% group_by(Area) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
areads
areads1<- data %>% select(Accident_Index,Area,Accident_Severity)%>% group_by(Area,Accident_Severity) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
areads1
data <- data %>%
mutate(spd_lt=case_when(Speed_limit %in% 1:20 ~"0-30",
Speed_limit %in% 20:30 ~ "30-60",
Speed_limit %in% 40:60 ~"60-100",
Speed_limit == 70 ~"100-130"))
table(data$spd_lt)
0-30 100-130 30-60 60-100
113875 28838 178895 96048
spdlt<- data %>% select(Accident_Index,spd_lt)%>% group_by(spd_lt) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
spdlt
spdlt1<- data %>% select(Accident_Index,spd_lt,Accident_Severity)%>% group_by(spd_lt,Accident_Severity) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
spdlt1
data <- data %>%
mutate(Hit_object_on_carraige_way =case_when(Hit_Object_in_Carriageway == 1 ~ 1,
Hit_Object_in_Carriageway == 2 ~ 0,
Hit_Object_in_Carriageway == 4 ~ 1,
Hit_Object_in_Carriageway == 5:7 ~ 0,
Hit_Object_in_Carriageway == 9:12 ~ 0,
Hit_Object_in_Carriageway == 8 ~ 1,
Hit_Object_in_Carriageway == 0 ~ 0,
Hit_Object_in_Carriageway == 2 ~ 0,))
table(data$Hit_object_on_carraige_way)
obstacle<- data %>% select(Accident_Index,Hit_object_on_carraige_way)%>% group_by(Hit_object_on_carraige_way) %>% drop_na(Hit_object_on_carraige_way)%>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
obstacle
obstacle1<- data %>% select(Accident_Index,Hit_object_on_carraige_way,Accident_Severity,)%>% group_by(Hit_object_on_carraige_way,Accident_Severity) %>% drop_na(Hit_object_on_carraige_way)%>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
obstacle1
data<-data%>%
filter(X1st_Point_of_Impact %in% 0:4)
table(data$X1st_Point_of_Impact)
impact<- data %>% select(Accident_Index,X1st_Point_of_Impact)%>% group_by(X1st_Point_of_Impact) %>% drop_na(X1st_Point_of_Impact)%>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
impact
impact1<- data %>% select(Accident_Index,X1st_Point_of_Impact,Accident_Severity)%>% group_by(X1st_Point_of_Impact,Accident_Severity) %>% drop_na(X1st_Point_of_Impact)%>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
impact1
pedestrian movement
data <- data %>%
mutate(Pedestrian_Mvt=case_when(Pedestrian_Movement %in% 1:4 ~1,
Pedestrian_Movement %in% 5:9 ~0,
Pedestrian_Movement == 0 ~0
))
table(data$Pedestrian_Mvt)
pedmove<- data %>% select(Accident_Index,Pedestrian_Mvt)%>% group_by(Pedestrian_Mvt)%>% drop_na(Pedestrian_Mvt)%>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
pedmove
pedmove1<- data %>% select(Accident_Index,Pedestrian_Mvt,Accident_Severity)%>% group_by(Pedestrian_Mvt,Accident_Severity)%>% drop_na(Pedestrian_Mvt)%>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
pedmove1
Casualty type
table(data$Casualty_Type)
casualty class this tow are interchangable and casaulty type
castyp<- data %>% select(Accident_Index,Casualty_Class)%>% group_by(Casualty_Class)%>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
castyp
castyp1<- data %>% select(Accident_Index,Casualty_Class,Accident_Severity)%>% group_by(Casualty_Class,Accident_Severity)%>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
castyp1
by variate analysis
highway_urban <- data %>% select(Accident_Index, Urban_or_Rural_Area, Highway )%>% group_by(Urban_or_Rural_Area, Highway ) %>% summarise(t_count = n())
highway_urban
chart_highway_urban<- ggplot(data=highway_urban, aes(x=Highway, y=t_count, fill=Urban_or_Rural_Area))+geom_bar(stat = "identity")+geom_text(aes(label=paste0(round(t_count, 2))), position = position_stack(vjust=0.9))+labs(y="Number of accidents", x= "Road type")+ scale_fill_discrete(name= "Area", labels=c("Urban", "Rural"))+
theme(legend.position="bottom", legend.direction="horizontal",
legend.title = element_blank())+ scale_fill_manual(values =c("#00BFFF", "#1E90FF"))
Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.
chart_highway_urban
area
areasev<- data %>% select(Accident_Index,Accident_Severity, Area)%>% group_by(Accident_Severity,Area) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
areasev
chart_areasev<- ggplot(data=areasev, aes(x=Accident_Severity, y=t_count, fill= Area))+geom_bar(stat = "identity")+geom_text(aes(label=paste0(round(t_count, 2), "%")), position = position_stack(vjust=0.9))+labs(y="Percentage", x= "Accident severity")+ scale_fill_discrete(name= "Area", labels=c("Higheway", "Urban", "Rural"))+
theme(legend.position="bottom", legend.direction="horizontal",
legend.title = element_blank())#+scale_fill_brewer(palette = "Set2") #scale_fill_manual(values =c("#00BFFF", "#1E90FF", "#4169E1"))
chart_areasev
manuevsev<- data %>% select(Accident_Index,Accident_Severity, Vehicle_Manoeuvre_category)%>% group_by(Vehicle_Manoeuvre_category, Accident_Severity) %>% summarise(t_count = n())%>% drop_na(Vehicle_Manoeuvre_category)%>% mutate(t_count = t_count/sum(t_count)*100)
manuevsev
ADAS classification
data <- data %>%
mutate(accident_type=case_when(Vehicle_Manoeuvre_category=="Going_ahead_Manoeuvre"& X1st_Point_of_Impact==1 & Number_of_Vehicles==2 & Junction_Detail==0 ~ "Rear-end collision",
Vehicle_Manoeuvre_category=="Turning_Manoeuvre" & X1st_Point_of_Impact %in% 3:4 & Number_of_Vehicles==2 &Junction_Detail %in% 1:9 ~ "Turning collision",
Vehicle_Manoeuvre_category=="Going_ahead_Manoeuvre" & Number_of_Vehicles==2 & X1st_Point_of_Impact %in% 2:3 &Junction_Detail %in% 1:9 ~ "Entering/crossing collision",
Vehicle_Manoeuvre_category=="Lane_change_Manoeuvre" & Hit_object_on_carraige_way==1 ~ "Collision with stationary traffic",
Vehicle_Manoeuvre_category=="Lane_change_Manoeuvre"& X1st_Point_of_Impact %in% 3:4 ~"collision with longitunal traffic",
Pedestrian_Mvt ==1 & Casualty_Class==3 ~ "Accident with pedestrian crossing the road",
Hit_object_on_carraige_way==1 & X1st_Point_of_Impact ==1 ~"Accident with other obsctacles on the carriageway"
))
table(data$accident_type)
Accident with other obsctacles on the carriageway Accident with pedestrian crossing the road collision with longitunal traffic
3547 50843 5272
Collision with stationary traffic Entering/crossing collision Rear-end collision
596 17666 31601
Turning collision
15309
acctype<- data %>% select(Accident_Index,accident_type )%>% group_by(accident_type) %>% summarise(t_count = n())%>%drop_na(accident_type)%>% mutate(t_count = t_count/sum(t_count)*100)
acctype
chart_acctype<- ggplot(data=acctype, aes(x=accident_type , y=t_count, fill= accident_type ))+geom_bar(stat = "identity")+geom_text(aes(label=paste0(round(t_count, 2), "%")),position=position_dodge(width=0.9), vjust=-0.25)
chart_acctype
acctypearea<- data %>% select(Accident_Index,Area,accident_type)%>% group_by(accident_type,Area) %>% drop_na(accident_type) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
acctypearea
chart_acctypearea<- ggplot(data=acctypearea, aes(x=accident_type, y=t_count, fill= Area ))+geom_bar(stat = "identity")+geom_text(aes(label=paste0(round(t_count, 2), "%")),position=position_stack(vjust=0.9))+labs(y="Percent %", x= "Accident Type")+ scale_fill_discrete(name= "Area", labels=c("Highway", "Urban", "Rural"))
chart_acctypearea
acctypspeed<- data %>% select(Accident_Index,spd_lt,accident_type)%>% group_by(accident_type,spd_lt) %>% drop_na(accident_type) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
acctypspeed
chart_acctypspeed<- ggplot(data=acctypspeed, aes(x=accident_type, y=t_count, fill= spd_lt ))+geom_bar(stat = "identity")+geom_text(aes(label=paste0(round(t_count, 2), "%")),position=position_stack(vjust=0.9))+labs(y="Percent %", x= "Accident Type") #scale_fill_discrete(name= "Area", labels=c("Highway", "Urban", "Rural"))
chart_acctypspeed
severity, just slight and fatal as ADAS can not do much about slight
acctypsev<- data %>% select(Accident_Index,Accident_Severity,accident_type)%>% group_by(accident_type,Accident_Severity) %>% drop_na(accident_type) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
acctypsev
chart_acctypsev<- ggplot(data=acctypsev, aes(x=accident_type, y=t_count, fill=Accident_Severity ))+geom_bar(stat = "identity")+geom_text(aes(label=paste0(round(t_count, 2), "%")),position=position_stack(vjust=0.9))+labs(y="Percent %", x= "Accident Type") #scale_fill_discrete(name= "Area", labels=c("Highway", "Urban", "Rural"))
chart_acctypsev
acctypall<- data %>% select(Accident_Index,Accident_Severity,accident_type, Area, spd_lt)%>% group_by(accident_type,Area, spd_lt,Accident_Severity) %>% drop_na(accident_type) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
acctypall
dataa1 <- data
filter(dataa1, Age_of_Vehicle < 5 & Accident_Severity !=3)
㤼㸱<㤼㸲 not meaningful for factors
NA
dataa1<-dataa1 %>%
filter(Age_of_Vehicle %in% 1:6 & Accident_Severity %in% 1:2)
acctypall2<- dataa1 %>% select(Accident_Index,Accident_Severity,accident_type, Area, spd_lt)%>% group_by(accident_type,Area, spd_lt,Accident_Severity) %>% drop_na(accident_type) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
acctypall2
acctype1<- dataa1 %>% select(Accident_Index,accident_type )%>% group_by(accident_type) %>% summarise(t_count = n())%>%drop_na(accident_type)%>% mutate(t_count = t_count/sum(t_count)*100)
acctype
chart_acctype1<- ggplot(data=acctype1, aes(x=accident_type , y=t_count, fill= accident_type ))+geom_bar(stat = "identity")+geom_text(aes(label=paste0(round(t_count, 2), "%")),position=position_dodge(width=0.9), vjust=-0.25)
chart_acctype1
acctypspeed1<- dataa1 %>% select(Accident_Index,spd_lt,accident_type)%>% group_by(accident_type,spd_lt) %>% drop_na(accident_type) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
acctypspeed1
chart_acctypspeed1<- ggplot(data=acctypspeed1, aes(x=accident_type, y=t_count, fill= spd_lt ))+geom_bar(stat = "identity")+geom_text(aes(label=paste0(round(t_count, 2), "%")),position=position_stack(vjust=0.9))+labs(y="Percent %", x= "Accident Type") #scale_fill_discrete(name= "Area", labels=c("Highway", "Urban", "Rural"))
chart_acctypspeed1
acctypsev1<- dataa1 %>% select(Accident_Index,Accident_Severity,accident_type)%>% group_by(accident_type,Accident_Severity) %>% drop_na(accident_type) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
acctypsev
chart_acctypsev1<- ggplot(data=acctypsev1, aes(x=accident_type, y=t_count, fill=Accident_Severity ))+geom_bar(stat = "identity")+geom_text(aes(label=paste0(round(t_count, 2), "%")),position=position_stack(vjust=0.9))+labs(y="Percent %", x= "Accident Type") #scale_fill_discrete(name= "Area", labels=c("Highway", "Urban", "Rural"))
chart_acctypsev1
acctypearea1<- dataa1 %>% select(Accident_Index,Area,accident_type)%>% group_by(accident_type,Area) %>% drop_na(accident_type) %>% summarise(t_count = n())%>% mutate(t_count = t_count/sum(t_count)*100)
acctypearea1
chart_acctypearea1<- ggplot(data=acctypearea1, aes(x=accident_type, y=t_count, fill= Area ))+geom_bar(stat = "identity")+geom_text(aes(label=paste0(round(t_count, 2), "%")),position=position_stack(vjust=0.9))+labs(y="Percent %", x= "Accident Type")+ scale_fill_discrete(name= "Area", labels=c("Highway", "Urban", "Rural"))
chart_acctypearea1